package com.game.manager;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import com.game.base.SimulationObject;
import com.game.performers.Actor;

public class LookUpManager implements Observer {
	
	private static LookUpManager instance;
	
	public static LookUpManager get(){
		if(instance == null){
			instance = new LookUpManager();
		}
		return instance;
	}
	public Hashtable<Integer, SimulationObject> objs = new Hashtable<Integer, SimulationObject>();
	
	public Hashtable<String, List<SimulationObject>> clsToMap = new Hashtable<String, List<SimulationObject>>();
	
	public boolean addSob(SimulationObject obj){
		if(objs.containsKey(obj.id)){
			return false;
		} else {
			objs.put(obj.id, obj);
			if(clsToMap.containsKey(obj.getClass().getSimpleName())){
				clsToMap.get(obj.getClass().getSimpleName()).add(obj);
			} else {
				ArrayList<SimulationObject> list = new ArrayList<SimulationObject>();
				list.add(obj);
				clsToMap.put(obj.getClass().getSimpleName(), list);
			}
			return true;
		}
	}
	
	public SimulationObject findSob(String attr, Object value, Class cls){
		List<SimulationObject> list = clsToMap.get(cls.getSimpleName());
		try {
			for(SimulationObject obj:list){
				Field field = obj.getClass().getField(attr);
				if(field.get(obj).equals(value)){
					return obj;
				}
			}
		} catch (Exception e) {
			
		}
		return null;
	}

	public void update(Observable o, Object arg) {
		
	}
}
